/**  定义Coach相关的接口 */
const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
// 引入mysql连接池
const pool = require("../utils/db.js");

// 新增教练
router.post("/coach/add", (req, resp)=>{
  let obj=req.body
  let sql = "insert into coach set ?"
  pool.query(sql, [obj], (error, result)=>{
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 注册成功
    resp.send(Response.ok());
  })
});

// 查询教练标签
router.get("/coach/label", (req, resp)=>{
  let sql = "select * from coach_label"
  pool.query(sql, (error, result)=>{
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 查询成功 返回标签
    resp.send(Response.ok({result}));
  })
});

// 查询科目类型
router.get("/coach/subjects",(req,resp)=>{
  let sql = "select * from subjects"
  pool.query(sql, (error, result)=>{
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 查询成功 返回标签
    resp.send(Response.ok({result}));
  })
})

// 查询教练列表
router.get("/coach/list",async (req, resp)=>{
  let { page, pagesize } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try{
    let sql = "select * from coach limit ?,?"
    let result=await pool.querySync(sql, [startIndex,size])
    let sql2="select count(*) as total from coach"
    let result2=await pool.querySync(sql2)
    resp.send(
      Response.ok({result,total:result2[0].total,pageIndex:page,pageSize:pagesize})
    )
  }
  catch(error){
    resp.send(Response.error(error))
  }
});

// 模糊查询教练列表
router.get("/coach/ByName", async (req, resp) => {
  let { page, pagesize, name } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try {
    let sql = `select * from coach where coach_name like '%${name}%' limit ?,?`;
    let result = await pool.querySync(sql, [startIndex, size]);
    let sql2 =
      `select count(*) as total from coach where coach_name like '%${name}%'`;
    let result2 = await pool.querySync(sql2);
    resp.send(
      Response.ok({
        result,
        total: result2[0].total,
        pageIndex: page,
        pageSize: pagesize,
      })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 通过id查询教练信息
router.get("/coach/ById",(req,resp)=>{
  let{coach_id}=req.query
  let sql = "select * from coach where coach_id=?"  
  pool.query(sql,[coach_id], (error, result)=>{
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 查询成功 返回标签
    resp.send(Response.ok({result}));
  })
})

// 教练评分排列
router.get("/coach/score", async (req, resp) => {
  let { page, pagesize } = req.query;
  let startIndex = (page - 1) * pagesize;
  let size = parseInt(pagesize);
  try {
    let sql = `SELECT * FROM coach ORDER BY coach_score DESC LIMIT ?,?`;
    let result = await pool.querySync(sql, [
      startIndex,
      size,
    ]);
    let sql2 = `SELECT count(*) as total FROM coach ORDER BY coach_score DESC`;
    let result2 = await pool.querySync(sql2,[]);
    resp.send(
      Response.ok({
        result,
        total: result2[0].total,
        pageIndex: page,
        pageSize: pagesize,
      })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 修改教练信息
router.post("/coach/update", (req, resp) => {
  let obj = req.body;
  let sql = "update coach set ? where coach_id=?";
  pool.query(sql, [obj,obj.coach_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    // 修改成功
    resp.send(Response.ok());
  });
});

// 将router对象导出
module.exports = router;